package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.FileService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.util.FileType;
import com.atguigu.lease.common.util.FileUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Tag(name = "文件管理模块")
@RestController
@RequestMapping("admin/file")
public class FileController {
    @Autowired
    FileService fileService;

    @Operation(summary = "图片上传")
    @PostMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        FileType fileType = FileUtil.getType(file.getInputStream());
        if(fileType==null){
            return Result.error(ResultCodeEnum.UPLOAD_DATA_ERROR);
        }
        String url = fileService.upload(file);

        return Result.ok(url);
    }
}
